﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMSPortal.UI;
using TMSPortal.Biz;
using NetFramework;
using Model = TMS.ProjectServiceModel;

namespace TMSPortal.Ajax.Project
{
    public partial class Project : AjaxPageBase
    {
        protected override object AjaxResult(string action)
        {
            resultBodyTitle = "项目管理";

            switch (action)
            {
                case "Create":
                    return Create();
                case "List":
                    return List();
                case "Delete":
                    return Delete();
                default:
                    resultBodyMessage = "无法获取项目信息";
                    break;
            }

            return null;
        }

        private Model.Project Create()
        {
            Model.Project project = SerializeHelper.DeserializeFromNameValueCollection<Model.Project>(Request.Form);
            if (project == null)
            {
                resultBodyMessage = "无法获取项目信息";
                return null;
            }
            else
            {
                Model.Project search = ProjectServiceBiz.Get(project.ID);
                if (search == null)
                {
                    return ProjectServiceBiz.Insert(project);
                }
                else
                {
                    resultBodyMessage = "项目已存在";
                    return null;
                }
            }
        }

        private List<Model.Project> List()
        {
            List<Model.Project> list = ProjectServiceBiz.GetAllProject();
            if (list == null)
            {
                list = new List<Model.Project>();
            }

            return list;
        }

        private object Delete()
        {
            Model.Project project = SerializeHelper.DeserializeFromNameValueCollection<Model.Project>(Request.QueryString);
            if (project == null)
            {
                resultShowType = ResultShowType.Error;
                resultBodyMessage = "无法获取项目信息";
                return null;
            }

            ProjectServiceBiz.Delete(project.ID);

            resultBodyMessage = "OK";
            resultShowType = ResultShowType.OK;
            resultBodyMessage = "删除成功";

            return null;
        }
    }
}